package com.higgins.eventtracking.client.health;

import java.util.ArrayList;
import java.util.List;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericStatsTab;
import com.higgins.eventtracking.client.health.components.GenericStatsTabParent;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;
import com.higgins.eventtracking.client.health.fundamentals.BodyChartPanel;

public class Secondary extends VerticalPanel	implements GenericTabInterface, GenericStatsTabParent	{
	private List<GenericTabInterface> listOfTabs = new ArrayList<GenericTabInterface>();
	private TabPanel tabPanel = new TabPanel();
	private GenericStatsTab bodyTab = new GenericStatsTab(this, "Body", 225);
	private final GenericStatsTab lawApplicationsTab = new GenericStatsTab(this, "Law Applications", 225);
	private boolean firstRun = true;
	private GenericStatsTabParent parent = null;
	private Integer tabIndex = null;
	private String tabTitle = null;
	
	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}
	@Override
	public void setTabIndex(Integer index) {
		this.tabIndex = index;
	}
	@Override
	public String getTabTitle()	{
		return tabTitle;
	}
	@Override
	public void setTabTitle(String tabTitle)	{
		this.tabTitle = tabTitle;
	}

	public Secondary(GenericStatsTabParent parent)	{
		this.parent = parent;
		int tabIndex = 0;
		bodyTab.setTabIndex(tabIndex++);
		bodyTab.setTabTitle("Body");
		bodyTab.addChartPanel(new BodyChartPanel());
		listOfTabs.add(bodyTab);

		lawApplicationsTab.setTabIndex(tabIndex++);
		lawApplicationsTab.setTabTitle("Law Applications");
		listOfTabs.add(lawApplicationsTab);
	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		populateTabPanel();
		addTabs();
		firstRun = false;
	}
	public void populateTabPanel()	{
		bodyTab.update();
		lawApplicationsTab.update();
		add(tabPanel);
	}
	private void addTabs()	{
		tabPanel.add(bodyTab, "Body", true);
		tabPanel.add(lawApplicationsTab, "Law Applications", true);				
		tabPanel.selectTab(0);
		tabPanel.addSelectionHandler(createSelectionHandler());		
	}
	public SelectionHandler<Integer> createSelectionHandler()	{
		SelectionHandler<Integer> selectionHandler = new SelectionHandler<Integer>() {
	        @Override
	        public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
            	int selectedIndex = integerSelectionEvent.getSelectedItem();
            	GenericTabInterface tab = listOfTabs.get(selectedIndex);
            	if (tab!=null)	{
            		tab.update();
            	}
	        }
		};
		return selectionHandler;
	}
	@Override
	public void updateTabTitle(Integer index, String title, Boolean alert) {
		TabBar tabBar = tabPanel.getTabBar();
		tabBar.setTabHTML(index, alert?"<div class=\"highlightAlertLabel\" style=\"width: "+ (title.length()*9) +"px;\">" + title + "</div>":title);
		if (alert)	{
			parent.updateTabTitle(this.tabIndex, tabTitle, true);
		}
	}
	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}

}
